package com.zhangtai.modules.service.impl.security;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.controller.admin.JobController;
import com.zhangtai.modules.dao.SecurityRecordDao;
import com.zhangtai.modules.dto.FundsRecordDto;
import com.zhangtai.modules.dto.SecurityRecordDto;
import com.zhangtai.modules.dto.SecurityRecordReportDto;
import com.zhangtai.modules.entity.employee.PromotionEntity;
import com.zhangtai.modules.entity.security.SecurityRecordEntity;
import com.zhangtai.modules.service.security.SecurityRecordService;
import com.zhangtai.modules.vo.SecurityRecordReportVo;
import com.zhangtai.modules.vo.SecurityRecordVo;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
public class SecurityRecordServiceImpl extends ServiceImpl<SecurityRecordDao, SecurityRecordEntity> implements SecurityRecordService {

    @Autowired
    private SecurityRecordDao securityRecordDao;
    @Autowired
    private JobController jobControl;

    @Override
    public PageResult queryPage(@RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<SecurityRecordEntity> page = this.page(
                new Page<SecurityRecordEntity>(pageNum,pageSize),
                new QueryWrapper<SecurityRecordEntity>().like(!StringUtils.isEmpty(keyword),"name",keyword)
                        .eq("is_delete",0)
        );

        return new PageResult(page);
    }


    @Override
    public PageResult<SecurityRecordDto> getRecordList(@RequestParam(value = "time", required = false) String time,
                                                       @RequestParam(value = "endTime", required = false) String endTime,
                                                   @RequestParam(value = "isCheck", required = false) Integer isCheck,
                                                   @RequestParam(value = "isConfirm", required = false) Integer isConfirm,
                                                   @RequestParam(value = "type", required = false) Integer type,
                                                   @RequestParam(value = "unitName", required = false) String unitName,
                                                   @RequestParam(value = "cityName",required = false) String cityName,
                                                   @RequestParam(value = "oid", required = false) String oid,
                                                   @RequestParam(value = "name",required = false) String name,
                                                   @RequestParam(value = "contractGoupIds",required = false) List<Long> contractGoupIds,
                                                       @RequestParam(value = "unitId",required = false) Long unitId,
                                                   @RequestParam(value = "pageNum",defaultValue = "1",required = false) Integer pageNum,
                                                   @RequestParam(value = "pageSize",defaultValue = "10",required = false ) Integer pageSize) {

        Page<SecurityRecordDto> page = new Page(pageNum,pageSize);
        List<SecurityRecordDto> entities = securityRecordDao.getRecord(time,endTime,isCheck,isConfirm,type,unitName,cityName,oid,name,contractGoupIds,unitId,page);

        page.setRecords(entities);
        return new PageResult<SecurityRecordDto>(page);

    }




    @Override
    public PageResult<SecurityRecordReportDto> getRecordReportList(
            @RequestParam(value = "contractIds",required = false) List<Long> contractIds,
            @RequestParam(value = "time", required = false) String time,
                                                             @RequestParam(value = "endTime", required = false) String endTime,
                                                             @RequestParam(value = "isConfirm", required = false) Integer isConfirm,
                                                                   @RequestParam(value = "name",required = false) String name,
                                                                   @RequestParam(value = "unitName", required = false) String unitName,
                                                             @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                                             @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize) {

        Page<SecurityRecordReportDto> page = new Page(pageNum,pageSize);
        isConfirm=1;
        List<SecurityRecordReportDto> entities = securityRecordDao.getRecordReport(contractIds,time,endTime,isConfirm,name,unitName,page);
        if(!CollectionUtils.isEmpty(entities)){
            for (SecurityRecordReportDto item:entities
                 ) {
                if(item.getJobSystemId()!=null) {
                    item.setJobSystem(jobControl.systemMap.get(item.getJobSystemId()));
                }
                if(item.getOrgId()!=null){
                    item.setOrgName(jobControl.orgMap.get(item.getOrgId()));
                }
                if(item.getUnitId()!=null){
                    item.setDepartMentName(jobControl.orgMap.get(item.getDepartMentName()));
                }


            }

        }
        page.setRecords(entities);
        return new PageResult<SecurityRecordReportDto>(page);

    }

    @Override
    public SecurityRecordEntity getByIdE(Long id) {
            SecurityRecordEntity one = this.getOne(new QueryWrapper<SecurityRecordEntity>().eq("record_id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public SecurityRecordEntity getByOidAndType(Long oid,Integer isCheck,String startDay,String endDay){
        SecurityRecordEntity one = this.getOne(new QueryWrapper<SecurityRecordEntity>().
                eq("oid", oid).eq("is_check", isCheck).between("operate_time",startDay,endDay).eq("is_delete",0));

        return one;
    }

    @Override
   public void removeUnCheckEntity(Long oid,String startDay,String endDay){
       List<SecurityRecordEntity> list = this.list(new QueryWrapper<SecurityRecordEntity>().
//               between("operate_time",startDay,endDay).
                eq("is_delete",0).eq("is_check",0).eq("oid",oid));
       for(SecurityRecordEntity record:list){
           removeOneById(record.getRecordId());
       }
   }

    @Override
    public void deleteAllRecordBySecurityId(Long securityId){
       securityRecordDao.deleteAllRecordBySecurityId(securityId);
    }
    public  List<String> getIdBySecurityId(Long securityId,Integer status){
        return securityRecordDao.getAllSecurity(securityId,status);
    }

    @Override
    public int getConfirmRecordCount(String securityId,String startTime,String endTime,Integer type){
        return securityRecordDao.getConfirmRecordCount(startTime,endTime,securityId,type);
    }

    @Override
    public R saveE(SecurityRecordEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(SecurityRecordEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            SecurityRecordEntity entity = new SecurityRecordEntity();
        entity.setRecordId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}